package com.hhdys.action;

import java.util.Date;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hhdys.annotation.Access;
import com.hhdys.model.MenuTree;
import com.hhdys.service.MenuTreeService;

@Controller
@RequestMapping("/admin/menu")
@Access("菜单管理")
public class MenuTreeAction {
	@Autowired
	private MenuTreeService menuTreeService;

	@RequestMapping("/menu")
	@Access("查看")
	public String getMenuJson(Model model) {
		String str = menuTreeService.getMenuTreesJson();
		model.addAttribute("json", str);
		return "admin/menu/menu";
	}

	@RequestMapping("/update")
	@ResponseBody
	@Access("修改")
	public String updateMenu(MenuTree menu) {
		Subject curUser = SecurityUtils.getSubject();
		menu.setLastModifier((String) curUser.getPrincipal());
		menu.setModifyTime(new Date());
		menuTreeService.updateMenu(menu);
		return "success";
	}

	@RequestMapping("/add")
	@ResponseBody
	@Access("添加")
	public String addMenu(MenuTree menu) {
		Subject curUser = SecurityUtils.getSubject();
		menu.setLastModifier((String) curUser.getPrincipal());
		menu.setModifyTime(new Date());
		menuTreeService.addMenu(menu);
		return "success";
	}

	@RequestMapping("/del")
	@ResponseBody
	@Access("删除")
	public String delMenu(int id) {
		menuTreeService.delMenu(id);
		return "success";
	}

	/**
	 * @return the menuTreeService
	 */
	public MenuTreeService getMenuTreeService() {
		return menuTreeService;
	}

	/**
	 * @param menuTreeService
	 *            the menuTreeService to set
	 */
	public void setMenuTreeService(MenuTreeService menuTreeService) {
		this.menuTreeService = menuTreeService;
	}

}
